package com.weidian.mobile.open.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.weidian.mobile.open.pojo.SaleMachine;
import com.weidian.mobile.open.repository.SaleMachineRepository;
import com.weidian.mobile.open.utils.HttpUtils;
import com.weidian.mobile.open.utils.StringUtils;

@RestController
public class SendTimeLineController {

	@Autowired
	StringRedisTemplate jedisTemplate;
	
	@Autowired
	SaleMachineRepository saleMachineRepository;
	
	@RequestMapping(value = "/sendtimeline", method = RequestMethod.POST)
	public String sendTimeLine(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String content = request.getParameter("content");
		String phoneId = request.getParameter("phoneId");
		String img1 = request.getParameter("img1");
		String img2 = request.getParameter("img2");
		String img3 = request.getParameter("img3");
		String img4 = request.getParameter("img4");
		String img5 = request.getParameter("img5");
		String img6 = request.getParameter("img6");
		String img7 = request.getParameter("img7");
		String img8 = request.getParameter("img8");
		String img9 = request.getParameter("img9");
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("timeline").append("weidian#ton")
			.append((content == null || content.isEmpty()) ? "" : content + "weidian#")
			.append((img1 == null || img1.isEmpty()) ? "" : img1 + "#ton")
			.append((img2 == null || img2.isEmpty()) ? "" : img2 + "#ton")
			.append((img3 == null || img3.isEmpty()) ? "" : img3 + "#ton")
			.append((img4 == null || img4.isEmpty()) ? "" : img4 + "#ton")
			.append((img5 == null || img5.isEmpty()) ? "" : img5 + "#ton")
			.append((img6 == null || img6.isEmpty()) ? "" : img6 + "#ton")
			.append((img7 == null || img7.isEmpty()) ? "" : img7 + "#ton")
			.append((img8 == null || img8.isEmpty()) ? "" : img8 + "#ton")
			.append((img9 == null || img9.isEmpty()) ? "" : img9 + "#ton");
		String token = jedisTemplate.opsForValue().get("esmobtoken");
		System.err.println(token);
		SaleMachine saleMachine = saleMachineRepository.findById(new Integer(phoneId));
		JSONObject jsonObject = new JSONObject();
		JSONObject jsonObject1 = new JSONObject();
		jsonObject1.put("type", "txt");
		jsonObject1.put("msg", StringUtils.encode(stringBuilder.toString()));
		jsonObject.put("target_type", "users");
		jsonObject.put("target", new String[]{StringUtils.getStrMD5(saleMachine.getMeid()).substring(0, 16)});
		jsonObject.put("msg", jsonObject1);
		String ret = HttpUtils.sendPostforEasemob("https://a1.easemob.com/1119161217115396/wonsionton/" + "messages", jsonObject.toJSONString(), "Bearer " + token);
		if (ret.contains("error")) {
			return "发送失败\n" + ret; 
		}
		return "成功";
	}
}
